﻿/* 关于 IB, 您可以参考 http://blog.sina.com.cn/s/blog_604c436d0100o5j0.html.
 * IB 是项目 zsharedcode 的一部分, 您可以参考 http://code.google.com/p/zsharedcode/ 或者 http://blog.sina.com.cn/s/blog_604c436d0100o04s.html.
 * Visual Studio 2010 项目模板请访问 http://blog.sina.com.cn/s/blog_604c436d0100o8gn.html.
 * 所有项目源码托管地址: http://panzer.codeplex.com/.
 * */

using System;
using System.Windows.Forms;

using zoyobar.shared.panzer.flow;
using zoyobar.shared.panzer.web.ib;

// HACK: 如运行有偏差，代码中的 jQuery 可能需要依据最新的 Google 或者 Bing 页面， IE 或者 jQuery 版本调整。或者注释掉 #define USELOCALSETTING，采用网络设置中的 jQuery。

namespace IBBloggerTemplate
{

	public partial class FormTemplate : Form
	{

		// 是否停止当前的搜索。
		private bool isStop;

		private Blog blog = null;

		public FormTemplate ( )
		{
			InitializeComponent ( );
		}

		private void ieConditionCompleted ( WebPageCondition condition, int conditionCount, int completedConditionCount )
		{ this.lblInfo.Text = string.Format ( "条件完成 {0} ({1}/{2}) 完成 ...", condition.Name, completedConditionCount, conditionCount ); }

		private void ieRemainWaitTimeChanged ( int remainSecond )
		{

			if ( remainSecond <= 0 )
				this.lblTime.Text = "...";
			else
				this.lblTime.Text = string.Format ( "等待 {0} 秒 ...", remainSecond );

		}

		private void ieStateCompleted ( FlowState<WebPageAction, WebPageCondition> state, ref bool isStop )
		{

			//	是否被用户停止，如果用户停止，则停止页面状态的转换，也就是停止搜索。
			if ( this.isStop )
			{
				isStop = true;
				return;
			}

			if ( state.Name == "public blog" )
				this.stopPublish ( );

			this.lblInfo.Text = string.Format ( "状态 {0} 完成 ...", state.Name );
		}

		private void stopPublish ( )
		{

			if ( null != this.blog )
				this.blog.IE.IEFlow.StopJump ( );

			this.isStop = true;

			this.menuPublish.Enabled = true;
			this.cmdPublish.Enabled = true;
			this.menuStop.Enabled = false;
			this.cmdStop.Enabled = false;

			this.lblInfo.Text = "...";
			this.lblTime.Text = "...";
		}

		private void publish()
		{

			if ( string.IsNullOrEmpty ( this.txtTitle.Text ) || this.htmlEditor.HTML == string.Empty || string.IsNullOrEmpty ( this.txtCatalog.Text ) || string.IsNullOrEmpty ( this.txtTag.Text ) || this.listBlog.SelectedIndex == -1 )
			{
				this.lblInfo.Text = "请将发布内容填写完整 ...";
				return;
			}

			this.isStop = false;

			this.menuPublish.Enabled = false;
			this.cmdPublish.Enabled = false;
			this.menuStop.Enabled = true;
			this.cmdStop.Enabled = true;

			// TODO: 可以添加新的继承自 Blog 的类。

			// 当选择不同的日志类型时，生成不同的类。
			switch ( this.listBlog.SelectedIndex )
			{
				case 0:
					// TODO: 这里可以填写自己的 sina blog 首页，用户名和密码。这里的测试账户也是可用的。
					blog = new SinaBlog ( this.webBrowser, "http://blog.sina.com.cn/iebrowser", "iebrowser@sina.cn", "iebrowser" );
					break;

				case 1:
					// TODO: 这里可以填写自己的 163 blog 首页，用户名和密码。这里的测试账户也是可用的。
					blog = new _163Blog ( this.webBrowser, "http://iebrowser.blog.163.com", "http://iebrowser.blog.163.com/blog/getBlog.do?fromString=blogmodule", "iebrowser@163.com", "iebrowser123" );
					break;

				default:
					this.blog = null;
					break;
			}

			if ( null == this.blog )
				return;

			// 当浏览器载入一个符合条件的地址时。
			this.blog.IE.IEFlow.ConditionCompleted += new ConditionCompletedEventHandler<WebPageCondition> ( this.ieConditionCompleted );
			// 当剩余等待时间改变时。
			this.blog.IE.IEFlow.RemainWaitTimeChanged += new RemainWaitTimeChangedEventHandler ( this.ieRemainWaitTimeChanged );

			// 当页面状态改变时。
			this.blog.IE.IEFlow.StateCompleted += new StateChangedEventHandler<WebPageAction, WebPageCondition> ( this.ieStateCompleted );

			// 发布日志。
			this.blog.WriteBlog ( this.txtTitle.Text, this.htmlEditor.HTML, this.txtCatalog.Text, this.txtTag.Text, true );

			this.split2.Panel1Collapsed = false;
			this.menuViewBrowser.Checked = true;
		}

		private void cmdExecuteJQuery_Click ( object sender, EventArgs e )
		{
			// 运行用户输入的 js 脚本。
			new IEBrowser ( this.webBrowser ).ExecuteScript ( this.txtJS.Text );
		}

		private void menuViewBrowser_Click ( object sender, EventArgs e )
		{ this.split2.Panel1Collapsed = !this.menuViewBrowser.Checked; }

		private void menuViewDebug_Click ( object sender, EventArgs e )
		{ this.split1.Panel2Collapsed = !this.menuViewDebug.Checked; }

		private void menuExit_Click ( object sender, EventArgs e )
		{ this.Close ( ); }

		private void menuStop_Click ( object sender, EventArgs e )
		{ this.stopPublish ( ); }

		private void menuPublish_Click ( object sender, EventArgs e )
		{ this.publish ( ); }

		private void cmdPublish_Click ( object sender, EventArgs e )
		{ this.publish ( ); }

		private void cmdStop_Click ( object sender, EventArgs e )
		{ this.stopPublish ( ); }

	}

}
